package com.hdy.orderforthree.mvp;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hdy.common.GoToSettings;
import com.hdy.orderforthree.R;
import com.hdy.orderforthree.R2;
import com.hdy.orderforthree.pos.ConcretePosFactory;
import com.hdy.orderforthree.pos.DGJPos;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TitleBarView extends BaseView implements ITitleBarContract.IView{

    TitleBarPresenter.ISwitchView mPresenterCallBack;

    @BindView(R2.id.start_order_tv)
    TextView mStartOrder;
    @BindView(R2.id.my_bill_tv)
    TextView mBill;
    @BindView(R2.id.tablets_info_tv)
    TextView mTabletsInfo;
    @BindView(R2.id.people_number_tv)
    TextView mNumberOfPeople;
    @BindView(R2.id.total_order_number)
    TextView mTotalOrderNumber;
    @BindView(R2.id.logo_image)
    ImageView mLogoImage;

    public TitleBarView (View view) {
        super(view);
        ButterKnife.bind(this, view);

        initView();
    }

    public void setPresenterCallBack(TitleBarPresenter.ISwitchView presenterCallBack) {
        mPresenterCallBack = presenterCallBack;
    }

    private void initView() {
        new GoToSettings(mLogoImage).tenClickEnter();
        mStartOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPresenterCallBack != null) {
                    mPresenterCallBack.showOrder();
                }
            }
        });

        mBill.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPresenterCallBack != null) {
                    mPresenterCallBack.showBill();
                }
            }
        });

        mNumberOfPeople.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPresenterCallBack != null) {
                    mPresenterCallBack.addPeopleNumber(true);
                }
            }
        });

        mTabletsInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPresenterCallBack != null) {
                    mPresenterCallBack.addLogin();
                }
            }
        });

        showTotalOrderNumberCorner();
    }

    public void showTotalOrderNumberCorner() {
        if (ConcretePosFactory.getInstance().getPos() instanceof DGJPos) {
            DGJPos pos = (DGJPos) ConcretePosFactory.getInstance().getPos();
            if (pos.getConfirmOrderRequest() != null) {
                mTotalOrderNumber.setText(String.valueOf((int) pos.getConfirmOrderRequest().getOrder().getTotalQty()));
                if (pos.getConfirmOrderRequest().getOrder().getTotalQty() == 0) {
                    mTotalOrderNumber.setVisibility(View.INVISIBLE);
                } else {
                    mTotalOrderNumber.setVisibility(View.VISIBLE);
                }
            } else {
                mTotalOrderNumber.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public void showOrder() {
        mStartOrder.setTextColor(mView.getResources().getColor(R.color.selectedTitleTextColor));
        mStartOrder.setBackground(mView.getResources().getDrawable(R.drawable.button_bg));

        mBill.setTextColor(mView.getResources().getColor(R.color.titleTextColor));
        mBill.setBackground(null);
    }

    @Override
    public void showBill() {
        mStartOrder.setTextColor(mView.getResources().getColor(R.color.titleTextColor));
        mStartOrder.setBackground(null);

        mBill.setTextColor(mView.getResources().getColor(R.color.selectedTitleTextColor));
        mBill.setBackground(mView.getResources().getDrawable(R.drawable.button_bg));
    }

    @Override
    public void showTableInfo() {
        String info = null;
        if (ConcretePosFactory.getInstance().getPos().getLoginBean() != null
                && ConcretePosFactory.getInstance().getPos().getLoginBean().getTables() != null) {
            info = ConcretePosFactory.getInstance().getPos().getLoginBean().getTables().getName();
        }
        String temp = mView.getResources().getString(R.string.tablets_info);
        if (info == null || info.equals("")) {
            info = mView.getResources().getString(R.string.empty_tablets);
        }
        mTabletsInfo.setText(String.format(temp,(String)info));
    }

    @Override
    public void showNumberOfPeople() {
        int number = 0;
        if (ConcretePosFactory.getInstance().getPos().getLoginBean() != null
                && ConcretePosFactory.getInstance().getPos().getLoginBean().getTables() != null) {
            number = ConcretePosFactory.getInstance().getPos().getLoginBean().getTables().getNumber();
        }
        String temp = mView.getResources().getString(R.string.number_of_people);
        mNumberOfPeople.setText(String.format(temp,(int)number));
    }

    @Override
    public void showCorner(int qty) {

    }
}
